//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData: [],
    price: "",
    orderNumber: "",
    // 
    payM: "",
    payRMB: "",
    payType: "",
    radioArr: [{
        img: "../../images/pay0.png",
        words: "余额",
        value: 0,
      },
      {
        img: "../../images/pay1.png",
        words: "微信",
        value: 2,
      }
    ],
    // 
    uid: "",
    userType: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options);
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
      price: options.price,
      orderNumber: options.orderNumber
    })
    this.GetUserInfoById();
  },

  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    var payM = this.data.payM;
    var payRMB = this.data.payRMB;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=PayAttractInvestment&OrderNumber=" + orderNumber + "&PayType=0" + "&PayMa=" + payM + "&PayRMB=" + payRMB),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == "1") {
                wx.navigateBack({
                  delta: 2
                })
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // GetUserInfoById
  GetUserInfoById: function() {
    var that = this;
    var uid = this.data.uid;
    var userData = this.data.userData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMyAccountInfo&UID=" + uid),
      success: function(res) {
        var userData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(userData);
        that.setData({
          userData: userData,
        })

      }
    })
  },
  // Minput
  Minput: function(e) {
    var that = this;
    var price = this.data.price;
    this.setData({
      payM: e.detail.value,
      payRMB: price - e.detail.value
    });
    if (e.detail.value > price) {
      wx.showToast({
        title: "请输入正确的M数！",
        icon: 'none',
        duration: 2000
      });
    }
  },
  // radioChange余额，微信
  radioChange: function(e) {
    console.log(e);
    this.setData({
      payType: e.detail.value,
    })
  },
  pay: function() {
    var that = this;
    var uid = this.data.uid;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var payM = this.data.payM;
    var payRMB = this.data.payRMB;
    var price = this.data.price;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    console.log(payType);
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (payM == '') {
      wx.showToast({
        title: "请输入要支付的M数！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (payM > price) {
      wx.showToast({
        title: "请输入正确的M数！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (payType == "0") {
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=PayAttractInvestment&OrderNumber=" + orderNumber + "&PayType=2" + "&PayMa=" + payM + "&PayRMB=" + payRMB),
        success: function(res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == "1") {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     console.log(res);
            //     wx.navigateBack({
            //       delta: 2
            //     })
            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })

          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    }

  },
  // 
})